<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: mindog<40120128@qq.com>
// +----------------------------------------------------------------------
namespace Admin\Controller;
use Admin\Model\AuthGroupModel;
use Think\Page;
use Vendor\Data;

/**
 * 后台内容控制器
 * @author mindog<40120128@qq.com>
 */
class DeptController extends AdminController {
	
	public function index(){
        /* 获取部门列表 */
        $map  = array('status' => array('gt', -1));
        $list = M('Dept')->where($map)->order('sort asc,id asc')->select();
		$mold_id=M('model')->getFieldByName('dept','id');
		
		//数字转换成文字
		$list=int_to_string($list);
		$dept_grands=get_attr_option($mold_id,"dept_grand");
		$list=int_to_string($list,array('dept_grand'=>$dept_grands));	

		//树形结构
		$list=Data::channelList($list,0,'<span class="tab-sign">|__</span>','id','pid');

		$this->assign('_list', $list);
		$this->assign('mold_id',$mold_id);
        $this->meta_title = '部门管理';
        $this->display();
	}
	
    public function edit(){
    	$dept = D('Dept');
		$mold_id=I('get.mold_id');
		$this->assign('mold_id',$mold_id);
		
    	if(IS_POST){
			$data=$dept->create();
			if($data){
				if($_GET['id']){
					if($dept->save()){
	                    $this->success('更新成功', U('index'));
	                } else {
	                    $this->error('更新失败');
	                }
				}else{
					if($dept->add()){
	                    $this->success('新增成功', U('index'));
	                } else {
	                    $this->error('新增失败');
	                }
				}
                
            } else {
                $this->error($dept->getError());
            }
    	}else{
    		if($_GET['id']){
				$info=M('dept')->find($_GET['id']);
				$this->assign('info',$info);
    		}
			//获取部门的等级可选项
    		$dept_grands=get_attr_option($mold_id,"dept_grand");	
			$this->assign('dept_grands',$dept_grands);
				
			//获取上级部门可选项	
			$map  = array('status' => array('gt', -1));
			$list = $this->lists('Dept', $map,'sort','id,pid,name');
			$list=Data::channelList($list,0,'<span class="tab-sign">|__</span>','id','pid');
			$this->assign('_list', $list);
			
			$status=get_attr_option($mold_id,"status");
			$this->assign('status',$status);
			
        	$this->meta_title = '部门管理';  
			 // 记录当前列表页的cookie
       		 Cookie('__forward__',$_SERVER['REQUEST_URI']);
			 
    		$this->display('edit');
    	}
    }
	
	public function del(){
		//todo
	}
}